package dao.oc_study;

import java.io.IOException;
import java.io.Writer;
import java.sql.SQLException;
import java.util.List;
import org.hibernate.Hibernate;
import org.hibernate.HibernateException;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.lob.SerializableClob;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import entitybean.oc_study.EntityBeanOc_Study;

public class DaoImpl_Oc_Study extends HibernateDaoSupport implements
		Dao_Oc_Study {
	public void Insert_Oc_Study(final EntityBeanOc_Study e0) {
		try {
			getHibernateTemplate().execute(new HibernateCallback() {
				@SuppressWarnings("deprecation")
				public Object doInHibernate(Session arg0)
						throws HibernateException, SQLException {	
					EntityBeanOc_Study e1 = e0;
					e1.setNews_clob(Hibernate.createClob(" "));
					arg0.save(e1);
					arg0.flush();
					arg0.refresh(e1, LockMode.UPGRADE);
					SerializableClob sclob = (SerializableClob) e1.getNews_clob();
					java.sql.Clob jclob = sclob.getWrappedClob();
					oracle.sql.CLOB clob = (oracle.sql.CLOB) jclob;
					try {
						Writer out = clob.getCharacterOutputStream();
						out.write(e1.getNews_clob_string());
						out.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
					return null;
				}
			});
		} catch (RuntimeException re) {
			throw re;
		}
		
		
	}

	public void Update_Oc_Study(EntityBeanOc_Study e) {
		this.getHibernateTemplate().update(e);
	}

	public void Delete_Oc_Study(EntityBeanOc_Study e) {
		this.getHibernateTemplate().delete(e);
	}

	@SuppressWarnings("unchecked")
	public void Delete_Oc_Study(String id) {
		List<EntityBeanOc_Study> list = this.getHibernateTemplate().find("from EntityBeanOc_Study where del='OFF' and id in("+ id + ")");
		for (int i = 0; i < list.size(); i++) {
			EntityBeanOc_Study e = list.get(i);
			e.setDel("ON");
			this.getHibernateTemplate().update(e);

		}

	}
	public EntityBeanOc_Study Find_Oc_Study_By_Id(String id)
	{
		EntityBeanOc_Study e=new EntityBeanOc_Study();
		List<EntityBeanOc_Study>list=this.getHibernateTemplate().find("from EntityBeanOc_Study where del='OFF' and id="+id);
		if(list.size()>0)
		{
			e=list.get(0);
		}
		return e;
	}

	public List<EntityBeanOc_Study> Find_EntityBeanOc_Study(final Integer top) {
		return getHibernateTemplate().executeFind( new HibernateCallback(){ 
			public Object doInHibernate(Session session) throws HibernateException, SQLException { 
			Query query=session.createQuery("from EntityBeanOc_Study  where del='OFF' order by id desc"); 
			query.setFirstResult(0);
			query.setMaxResults(top); 
			return query.list(); 
			} 
	   });
	}
	
	
}
